<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Mdl_member extends CI_Model {
	function __construct() {
		parent::__construct();
	}

	function get_table() {
		$table = "Member";

		return $table;
	}

	//we also can do
	//var $table = "dfasfs";
	//and inside each function, we will change $table by $this->table

	function get($order_by) {
		$table = $this->get_table();

		$this->db->order_by($order_by);
		$query = $this->db->get($table);

		return $query;
	}

	function get_with_limit($limit, $offset, $order_by) {
		$table = $this->get_table();
		$this->db->limit($limit, $offset);
		$this->db->order_by($order_by);
		$query = $this->db->query($table);

		return $query;
	}

	function get_where($id) {
		$table = $this->get_table();
		$this->db->where('id', $id);
		$query = $this->db->get($table);

		return $query;
	}

	function get_where_custom($col, $value) {
		$table = $this->get_table();
		$this->db->where($col, $value);
		$query = $this->db->get($table);

		return $query;
	}

	function _insert($data) {
		$table = $this->get_table();
		$result = $this->db->insert($table, $data);

		$insert_id = $this->db->insert_id();
		$this->db->trans_complete();
		return  $insert_id;
	}

	function _update($id, $data) {
		$table = $this->get_table();
		$this->db->where('id', $id);
		$this->db->update($table, $data);
	}

	function _delete($id) {
		$table = $this->get_table();
		$this->db->where('id', $id);
		$this->db->delete($table);
	}

	function count_where($column, $value) {
		$table = $this->get_table();
		$this->db->where($column, $value);
		$query = $this->db->get($table);
		$num_rows = $query->num_rows();
		return $num_rows;
	}

	function count_all() {
		$table = $this->get_table();
		$query = $this->db->get($table);
		$num_rows = $query->num_rows();

		return $num_rows;
	}

	function get_max() {
		$table = $this->get_table();
		$this->db->select_max('id');
		$query = $this->db->get($table);
		$row = $query->row();
		$id = $row->id;
		return $id;
	}

	function _custom_query($mysql_query) {
		$query = $this->db->query($mysql_query);
		return $query;
	}

	function getAgentInfo($pro_dir_id, $lang_id, $market) {
		$this->db->select('u.User_ID,u.BusinessName,u.Mobile,u.Email,u.CEA_RegistrationNo,u.CEA_LicenceNo');
		$this->db->from('dbo.Users u');
		$this->db->join('dbo.Listings l', 'u.User_ID = l.AgentFID');

		$where = array(
        	'l.Listing_ID'	=> $pro_dir_id,
        	'u.MarketFID'   => $this->db->escape_str($market)
		);

		$this->db->where($where);

		$query = $this->db->get();

		if ($query->num_rows() > 0) {
			return $query->row();
		} else {
			return false;
		}
	}
	
	function getAgentInfo2($pro_dir_id, $lang_id, $market) {
		$str = "select u.*
				from
					(
						select pa.AgentFID
						from dbo.PrjDirectory pd
						join dbo.PrjDir_Agent pa ON pa.PrjDirectoryFID = pd.ID
						where pd.ID = ". $this->db->escape($pro_dir_id) ."
					) as a 
				join dbo.Users u ON u.User_ID = a.AgentFID";
		
		$query = $this->db->query($str);
		
		if ($query->num_rows() > 0) {
			return $query->result();
		} else {
			return false;
		}
		
		/*
		$this->db->select('u.BusinessName,u.Mobile,u.Email');
		$this->db->from('dbo.Users u');
		$this->db->join('dbo.PrjDirectory l', 'u.User_ID = l.AgentFID');

		$where = array(
        	'l.ID'	=> $pro_dir_id,
        	'u.MarketFID'   => $this->db->escape_str($market)
		);

		$this->db->where($where);

		$query = $this->db->get();

		if ($query->num_rows() > 0) {
			return $query->row();
		} else {
			return false;
		}
		*/
	}

	function getTotalMemberByEmail($email) {
		$table = $this->get_table();
		 
		$this->db->select();
		$this->db->from($table);

		$where = array(
        	'Email'			=> $this->db->escape_str(trim($email)),
			'MarketFID'		=> $this->db->escape_str(trim($this->config->item('market')))
		);

		$this->db->where($where);

		$query = $this->db->get();

		if ($query->num_rows() > 0) {
			return true;
		} else {
			return false;
		}
	}

	/*
	 * Author: Chi Nguyen
	 * get list all location by city id
	 *
	 * @access public
	 * @param integer
	 * @param integer
	 * @param string
	 * @return array
	 */
	function getLocationByCityId($LanguageId, $CityId, $market) {
		$this->db->select('l.Location_ID,l.Name,l.CityFID,l.LanguageFID');
		$this->db->from('dbo.Location l');
		$this->db->join('dbo.City c', 'l.CityFID = c.City_ID'); // this joins the user table to topics
		$this->db->join('dbo.Country co', 'co.Country_ID = c.CountryFID'); // this joins the quote table to the topics table
		$where = array('l.Visible' => 1, 'l.LanguageFID' => $LanguageId, 'l.CityFID' => $CityId, 'co.Code' => $market);
		$this->db->where($where);
		$this->db->order_by('l.Name');

		$query = $this->db->get();

		return $query->result();
	}
	
	function getCustomer($customer_id) {
		$query = $this->db->query("SELECT * FROM Member WHERE Member_ID = '" . (int)$customer_id . "'");
		
		return $query->row();
	}
	
	function editCustomer($data) {
		$this->db->query("UPDATE Member SET Title = '". $this->db->escape_str($data['Title']) ."', FirstName = '" . $this->db->escape_str($data['FirstName']) . "', LastName = '" . $this->db->escape_str($data['LastName']) . "', Email = '" . $this->db->escape_str($data['Email']) . "', MobileCountry = '" . $this->db->escape_str($data['MobileCountry']) . "', Mobile = '" . $this->db->escape_str($data['Mobile']) . "' WHERE Member_ID = '" . (int)$this->members->getId() . "'");
	}
	
	function editPassword($email, $password) {
		$str = "UPDATE Member SET Salt = '" . $this->db->escape_str($salt = substr(md5(uniqid(rand(), true)), 0, 9)) . "', PassWord = '" . $this->db->escape_str(sha1($salt . sha1($salt . sha1($password)))) . "' WHERE Email = '" . $this->db->escape_str(utf8_strtolower($email)) . "' AND MemberGroupFID = '1' AND MarketFID = '". $this->db->escape_str(trim($this->config->item('market'))) ."'";
		
      	$this->db->query($str);
	}
}

?>
